package me.ljd.yigo.funext;

import java.util.Arrays;

import com.bokesoft.yigo.common.def.SystemField;
import com.bokesoft.yigo.meta.dataobject.MetaTable;
import com.bokesoft.yigo.meta.form.MetaForm;
import com.bokesoft.yigo.mid.base.DefaultContext;
import com.bokesoft.yigo.mid.connection.IDBManager;
import com.bokesoft.yigo.mid.parser.BaseMidFunctionImpl;
import com.bokesoft.yigo.parser.BaseFunImplCluster;
import com.bokesoft.yigo.parser.IExecutor;
import com.bokesoft.yigo.struct.datatable.DataTable;
import com.bokesoft.yigo.struct.datatable.DataTableMetaData;
import com.bokesoft.yigo.tools.document.DataTableUtil;

import me.ljd.yigo.formula.DBQuery2Formula;
import me.ljd.yigo.utils.DBManagerUtil2;

public class MidFunctionsExt extends BaseFunImplCluster{
	class DSNQuery extends BaseMidFunctionImpl {
		private DataTable dataTable;

		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) {
			String dsn = (String)args[0];
			String sql = (String)args[1];
			try {
				IDBManager dbManager=DBManagerUtil2.getDBManager(dsn);
				this.dataTable = dbManager.execPrepareQuery(sql, Arrays.copyOfRange(args, 2,args.length));
				//dbManager.close();
			} catch (Throwable e) {
				e.printStackTrace();
				throw new IllegalArgumentException("数据查询出错");
			}
			this.dataTable.batchUpdate();
			return this.dataTable;
		}
		
	}

	class DSNQueryG extends BaseMidFunctionImpl {
		private DataTable tableNew;

		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable{
			String dsn = (String)args[0];
			String sql = (String)args[1];
			tableNew = DBManagerUtil2.getTableNew(context);
			if(sql==null) return tableNew;
			try {
				IDBManager dbManager=DBManagerUtil2.getDBManager(dsn);
				DataTable dataTable = dbManager.execPrepareQuery(sql, Arrays.copyOfRange(args, 2,args.length));
				//dbManager.close();//关闭后会导致查询报错
				convertG(dataTable, tableNew);
			} catch (Throwable e) {
				DBManagerUtil2.dsnMapClear();//调试改动会导致dbManager关掉，所以在此清空
				e.printStackTrace();
				throw new IllegalArgumentException("数据查询出错");
			}
			this.tableNew.batchUpdate();
			return this.tableNew;
		}
	}
	class DSNQuery2G extends BaseMidFunctionImpl {
		private DataTable tableNew;

		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable{
			String sql = (String)args[0];
			tableNew = DBManagerUtil2.getTableNew(context);
			if(sql==null) return tableNew;
			try {
				IDBManager dbManager=DBManagerUtil2.getDBManager(1);
				DataTable dataTable = dbManager.execPrepareQuery(sql, Arrays.copyOfRange(args, 1,args.length));
				//dbManager.close();//关闭后会导致查询报错
				convertG(dataTable, tableNew);
			} catch (Throwable e) {
				System.out.println(sql);
				DBManagerUtil2.dsnMapClear();//调试改动会导致dbManager关掉，所以在此清空
				e.printStackTrace();
				throw new IllegalArgumentException("数据查询出错");
			}
			this.tableNew.batchUpdate();
			return this.tableNew;
		}
	}	
	class DBQueryG extends BaseMidFunctionImpl {
		private DataTable tableNew;

		public Object evalImpl(String name, DefaultContext context, Object[] args, IExecutor executor) throws Throwable{
			String sql = (String)args[0];
			tableNew = DBManagerUtil2.getTableNew(context);
//			tableNew = doc.get(0);
			if(sql==null) return tableNew;
			try {
				IDBManager dbManager=context.getDBManager();
				DataTable dataTable = dbManager.execPrepareQuery(sql, Arrays.copyOfRange(args, 1,args.length));
				convertG(dataTable, tableNew);
			} catch (Throwable e) {
				e.printStackTrace();
				throw new IllegalArgumentException("数据查询出错");
			}
			this.tableNew.batchUpdate();
			return this.tableNew;
		}
		
	}
	private void convertG(DataTable dataTable,DataTable tableNew) {
		DataTableMetaData meta = dataTable.getMetaData();
		Long rowKey = 0L;
		String colKey = null;
		Long oid = 0L;
		int soid = meta.getColumnCount();
		dataTable.beforeFirst();
		while(dataTable.next()) {
			rowKey ++;
			if(meta.constains(SystemField.OID_SYS_KEY)) {
				rowKey = dataTable.getLong(SystemField.OID_SYS_KEY);
			}
			for (int i = 0; i < meta.getColumnCount(); i++) {
				colKey = meta.getColumnInfo(i).getColumnKey();
				if(meta.constains("Row_Key") && colKey.equalsIgnoreCase(SystemField.OID_SYS_KEY)) continue;
				tableNew.append();
				if(meta.constains("Row_Key")) tableNew.setLong("Row_Key", rowKey);
				tableNew.setString("Col_Name", colKey);
				tableNew.setObject("Col_Value", dataTable.getObject(i));
				oid++;
				tableNew.setObject(SystemField.OID_SYS_KEY, oid);
				tableNew.setObject(SystemField.SOID_SYS_KEY, soid);
			}
		}
	}
	
	@Override
	public Object[][] getImplTable() {
		System.out.println("MidFunctionsExt.getImplTable()");
		return new Object[][]{
			{"DSNQuery", new DSNQuery()},
			{"DSNQueryG", new DSNQueryG()},
			{"DBQueryG", new DBQueryG()},
			{"DBQuery2", new DBQuery2Formula()},
			{"DBQuery2G", new DSNQuery2G()}
		};
	}

}
